package cn.ibizlab.odoo.core.odoo_stock.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Istock_rule;
import cn.ibizlab.odoo.core.odoo_stock.domain.Stock_rule;
import cn.ibizlab.odoo.core.odoo_stock.filter.Stock_ruleSearchContext;
import cn.ibizlab.odoo.core.odoo_stock.service.IStock_ruleService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[库存规则] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Stock_ruleServiceImpl implements IStock_ruleService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Istock_ruleClientService stock_ruleClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //stock_ruleClientService.removeBatch(stock_rules) ;

    }

    @Override
    public Stock_rule get(Integer id) {
        Istock_rule clientModel = stock_ruleClientService.createModel();
        clientModel.setId(id);
        stock_ruleClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Stock_rule get(Stock_rule stock_rule) {
        Stock_rule retstock_rule = this.get(stock_rule.getId()) ;
    	BeanCopier copier=BeanCopier.create(retstock_rule.getClass(), stock_rule.getClass(), false);
        copier.copy(retstock_rule, stock_rule, null);
        return stock_rule ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Istock_rule clientModel = stock_ruleClientService.createModel();
        clientModel.setId(id);
        stock_ruleClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Stock_rule> stock_rules, int batchSize) {

        //stock_ruleClientService.updateBatch(stock_rules,batchSize) ;

    }

    @Override
    public boolean update(Stock_rule stock_rule) {
        boolean bOk = false ;
        Istock_rule clientModel = this.convert2Model(stock_rule,null);
        stock_ruleClientService.update(clientModel) ;
        this.convert2Domain(clientModel,stock_rule) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Stock_rule stock_rule) {
        boolean bOk = false ;
        Istock_rule clientModel = this.convert2Model(stock_rule,null);
        stock_ruleClientService.create(clientModel) ;
        this.convert2Domain(clientModel,stock_rule);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Stock_rule> stock_rules, int batchSize) {

                        //stock_ruleClientService.createBatch(stock_rules,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Stock_rule> searchDefault(Stock_ruleSearchContext context) {
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据公司查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_company(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_partner_address(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_create(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_write(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存路线查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_route(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_location(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_location_src(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据拣货类型查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_picking_type(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据仓库查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_propagate_warehouse(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据仓库查询
     */
    @Override
    public Page<Stock_rule> selectByOdoo_warehouse(Integer id){
        Stock_ruleSearchContext context = new Stock_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rule> list = new ArrayList<Stock_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rule> clientModelList = stock_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Istock_rule convert2Model(Stock_rule domain , Istock_rule model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = stock_ruleClientService.createModel() ;
        }

         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getProcure_methodDirtyFlag())
            model.setProcure_method(domain.getProcure_method());
         if(domain.getPropagateDirtyFlag())
            model.setPropagate(domain.getPropagate());
         if(domain.getGroup_idDirtyFlag())
            model.setGroup_id(domain.getGroup_id());
         if(domain.getRule_messageDirtyFlag())
            model.setRule_message(domain.getRule_message());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getAutoDirtyFlag())
            model.setAuto(domain.getAuto());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDelayDirtyFlag())
            model.setDelay(domain.getDelay());
         if(domain.getGroup_propagation_optionDirtyFlag())
            model.setGroup_propagation_option(domain.getGroup_propagation_option());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getActionDirtyFlag())
            model.setAction(domain.getAction());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getPartner_address_id_textDirtyFlag())
            model.setPartner_address_id_text(domain.getPartner_address_id_text());
         if(domain.getLocation_id_textDirtyFlag())
            model.setLocation_id_text(domain.getLocation_id_text());
         if(domain.getRoute_id_textDirtyFlag())
            model.setRoute_id_text(domain.getRoute_id_text());
         if(domain.getRoute_sequenceDirtyFlag())
            model.setRoute_sequence(domain.getRoute_sequence());
         if(domain.getPicking_type_id_textDirtyFlag())
            model.setPicking_type_id_text(domain.getPicking_type_id_text());
         if(domain.getPropagate_warehouse_id_textDirtyFlag())
            model.setPropagate_warehouse_id_text(domain.getPropagate_warehouse_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getLocation_src_id_textDirtyFlag())
            model.setLocation_src_id_text(domain.getLocation_src_id_text());
         if(domain.getWarehouse_id_textDirtyFlag())
            model.setWarehouse_id_text(domain.getWarehouse_id_text());
         if(domain.getLocation_idDirtyFlag())
            model.setLocation_id(domain.getLocation_id());
         if(domain.getPartner_address_idDirtyFlag())
            model.setPartner_address_id(domain.getPartner_address_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getWarehouse_idDirtyFlag())
            model.setWarehouse_id(domain.getWarehouse_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getPicking_type_idDirtyFlag())
            model.setPicking_type_id(domain.getPicking_type_id());
         if(domain.getRoute_idDirtyFlag())
            model.setRoute_id(domain.getRoute_id());
         if(domain.getLocation_src_idDirtyFlag())
            model.setLocation_src_id(domain.getLocation_src_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getPropagate_warehouse_idDirtyFlag())
            model.setPropagate_warehouse_id(domain.getPropagate_warehouse_id());
        return model ;
    }

    public Stock_rule convert2Domain( Istock_rule model ,Stock_rule domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Stock_rule();
        }

        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getProcure_methodDirtyFlag())
            domain.setProcure_method(model.getProcure_method());
        if(model.getPropagateDirtyFlag())
            domain.setPropagate(model.getPropagate());
        if(model.getGroup_idDirtyFlag())
            domain.setGroup_id(model.getGroup_id());
        if(model.getRule_messageDirtyFlag())
            domain.setRule_message(model.getRule_message());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getAutoDirtyFlag())
            domain.setAuto(model.getAuto());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDelayDirtyFlag())
            domain.setDelay(model.getDelay());
        if(model.getGroup_propagation_optionDirtyFlag())
            domain.setGroup_propagation_option(model.getGroup_propagation_option());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getActionDirtyFlag())
            domain.setAction(model.getAction());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getPartner_address_id_textDirtyFlag())
            domain.setPartner_address_id_text(model.getPartner_address_id_text());
        if(model.getLocation_id_textDirtyFlag())
            domain.setLocation_id_text(model.getLocation_id_text());
        if(model.getRoute_id_textDirtyFlag())
            domain.setRoute_id_text(model.getRoute_id_text());
        if(model.getRoute_sequenceDirtyFlag())
            domain.setRoute_sequence(model.getRoute_sequence());
        if(model.getPicking_type_id_textDirtyFlag())
            domain.setPicking_type_id_text(model.getPicking_type_id_text());
        if(model.getPropagate_warehouse_id_textDirtyFlag())
            domain.setPropagate_warehouse_id_text(model.getPropagate_warehouse_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getLocation_src_id_textDirtyFlag())
            domain.setLocation_src_id_text(model.getLocation_src_id_text());
        if(model.getWarehouse_id_textDirtyFlag())
            domain.setWarehouse_id_text(model.getWarehouse_id_text());
        if(model.getLocation_idDirtyFlag())
            domain.setLocation_id(model.getLocation_id());
        if(model.getPartner_address_idDirtyFlag())
            domain.setPartner_address_id(model.getPartner_address_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getWarehouse_idDirtyFlag())
            domain.setWarehouse_id(model.getWarehouse_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getPicking_type_idDirtyFlag())
            domain.setPicking_type_id(model.getPicking_type_id());
        if(model.getRoute_idDirtyFlag())
            domain.setRoute_id(model.getRoute_id());
        if(model.getLocation_src_idDirtyFlag())
            domain.setLocation_src_id(model.getLocation_src_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getPropagate_warehouse_idDirtyFlag())
            domain.setPropagate_warehouse_id(model.getPropagate_warehouse_id());
        return domain ;
    }


}
